package com.aibili.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.HelpInfo;
import com.aibili.pojo.NoticeInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.DsConstant;

@Service("noticeInfoService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class NoticeInfoService extends BaseService {

	/**
	 * 
	 * @Description 通过 user_id 查找通知消息
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<NoticeInfo> getByUserid(long user_id) {
		String hql = " from NoticeInfo where from_id = :from_id or user_id=0 order by publish_time desc";
		List<NoticeInfo> infos = new ArrayList<NoticeInfo>();
		infos = (List<NoticeInfo>) dataDao.getObjectsViaParam(hql, new String[] { "from_id" }, user_id);
		return infos;
	}

	/**
	 * 
	 * @Description 条件查找消息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<NoticeInfo> getByParams(Integer pageIndex, Integer pageSize) {
		String orderhql = " order by publish_time desc";
		String wherehql = " where del_flg = 0 and user_id = 0 ";
		String chql = " select count(g) from NoticeInfo g " + wherehql;
		;

		PageBodyModel<NoticeInfo> result = new PageBodyModel<NoticeInfo>();
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		if (count > 0) {
			String hql = " from NoticeInfo " + wherehql + orderhql;
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<NoticeInfo> banners = new ArrayList<NoticeInfo>();
			banners = (List<NoticeInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			result = new PageBodyModel<>(page, banners);
		}

		return result;
	}

	/**
	 * 
	 * @Description 新增消息
	 * @param noticeInfo
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addNoticeInfo(NoticeInfo noticeInfo) {
		dataDao.addObject(noticeInfo);
	}

	/**
	 * 
	 * @Description 通过 id 删除消息 (假删)
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteById(long id) {
		NoticeInfo info = dataDao.getObjectById(NoticeInfo.class, id);
		if (info != null) {
			info.setDel_flag(1);
			dataDao.updateObject(info);
		}
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<HelpInfo> getHelplist(Integer pageIndex, int pagesize) {
		String chql = " select count(id) from HelpInfo";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<HelpInfo> result = new PageBodyModel<>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, DsConstant.PAGESIZE);
			String hql = " from HelpInfo order by addtime desc";
			List<HelpInfo> infos = new ArrayList<>();
			infos = (List<HelpInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page.getPageIndex(), null);
			result = new PageBodyModel<>(page, infos);
		}
		return result;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addHelpInfo(HelpInfo helpInfo) {
		dataDao.addObject(helpInfo);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteHelpById(long id) {
		HelpInfo helpInfo = dataDao.getObjectById(HelpInfo.class, id);
		if (helpInfo!=null) {
			dataDao.deleteObject(helpInfo);
		}
		
	}

}
